In [1]:
%matplotlib inline
from ggplot import *

Making Multiple Plots

Making multiple plots at the same time is easy in ggplot. Plots can either be show inline using the print function, the __repr__ (what python prints out by default for something), or ggplot's save function.

Since we're trying to make multiple plots, the __repr__ example doesn't really appy here, so we'll be focusing on using print and save to make multiple plots.

Let's say you want to generate a histogram of price data for each type of diamond cut in the diamonds dataset. First thing you need to do is split your dataset up into groups. You can do this using the groupby function in pandas.


In [2]:
for i, group in diamonds.groupby("cut"):
    print group.head()


     carat   cut color clarity  depth  table  price     x     y     z
8     0.22  Fair     E     VS2   65.1   61.0    337  3.87  3.78  2.49
91    0.86  Fair     E     SI2   55.1   69.0   2757  6.45  6.33  3.52
97    0.96  Fair     F     SI2   66.3   62.0   2759  6.27  5.95  4.07
123   0.70  Fair     F     VS2   64.5   57.0   2762  5.57  5.53  3.58
124   0.70  Fair     F     VS2   65.3   55.0   2762  5.63  5.58  3.66
    carat   cut color clarity  depth  table  price     x     y     z
2    0.23  Good     E     VS1   56.9   65.0    327  4.05  4.07  2.31
4    0.31  Good     J     SI2   63.3   58.0    335  4.34  4.35  2.75
10   0.30  Good     J     SI1   64.0   55.0    339  4.25  4.28  2.73
17   0.30  Good     J     SI1   63.4   54.0    351  4.23  4.29  2.70
18   0.30  Good     J     SI1   63.8   56.0    351  4.23  4.26  2.71
    carat    cut color clarity  depth  table  price     x     y     z
0    0.23  Ideal     E     SI2   61.5   55.0    326  3.95  3.98  2.43
11   0.23  Ideal     J     VS1   62.8   56.0    340  3.93  3.90  2.46
13   0.31  Ideal     J     SI2   62.2   54.0    344  4.35  4.37  2.71
16   0.30  Ideal     I     SI2   62.0   54.0    348  4.31  4.34  2.68
39   0.33  Ideal     I     SI2   61.8   55.0    403  4.49  4.51  2.78
    carat      cut color clarity  depth  table  price     x     y     z
1    0.21  Premium     E     SI1   59.8   61.0    326  3.89  3.84  2.31
3    0.29  Premium     I     VS2   62.4   58.0    334  4.20  4.23  2.63
12   0.22  Premium     F     SI1   60.4   61.0    342  3.88  3.84  2.33
14   0.20  Premium     E     SI2   60.2   62.0    345  3.79  3.75  2.27
15   0.32  Premium     E      I1   60.9   58.0    345  4.38  4.42  2.68
    carat        cut color clarity  depth  table  price     x     y     z
5    0.24  Very Good     J    VVS2   62.8   57.0    336  3.94  3.96  2.48
6    0.24  Very Good     I    VVS1   62.3   57.0    336  3.95  3.98  2.47
7    0.26  Very Good     H     SI1   61.9   55.0    337  4.07  4.11  2.53
9    0.23  Very Good     H     VS1   59.4   61.0    338  4.00  4.05  2.39
19   0.30  Very Good     J     SI1   62.7   59.0    351  4.21  4.27  2.66

Now to create and render a plot for each one of these subgroups, just make a ggplot object add a geom_histogram layer, and then print the plot.


In [3]:
for name, group in diamonds.groupby("cut"):
    p = ggplot(group, aes(x='price')) + geom_histogram() + ggtitle(name)
    print(p)


<ggplot: (270895005)>
<ggplot: (285808485)>
<ggplot: (285107893)>
<ggplot: (285840221)>
<ggplot: (289914273)>

If you want to save the plots to a file instead, just use save instead of print.


In [4]:
for name, group in diamonds.groupby("cut"):
    p = ggplot(group, aes(x='price')) + geom_histogram() + ggtitle(name)
    filename = "price-distribution-" + name + ".png"
    p.save(filename)



In [ ]: